package com.it.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.domain.Customer;
import com.it.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/customers")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @GetMapping
    public ResponseEntity<Map<String, Object>> getAllCustomers(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String contactPerson,
            @RequestParam(required = false) String contactEmail,
            @RequestParam(required = false) String contactPhone,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate
    ) {

        // 创建分页对象
        Page<Customer> pageInfo = new Page<>(page, pageSize);

        // 创建查询条件
        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();

        // 客户名称条件
        if (name != null && !name.isEmpty()) {
            queryWrapper.eq(Customer::getName, name);
        }
        if (contactEmail != null && !contactEmail.isEmpty()) {
            queryWrapper.eq(Customer::getContactEmail, contactEmail);
        }
        if (contactPhone != null && !contactPhone.isEmpty()) {
            queryWrapper.eq(Customer::getContactPhone, contactPhone);
        }
        // 状态条件
        if (contactPerson != null && !contactPerson.isEmpty()) {
            queryWrapper.eq(Customer::getContactPerson, contactPerson);
        }

        // 日期范围条件
        if (startDate != null && endDate != null) {
            queryWrapper.between(Customer::getUpdateTime, LocalDate.parse(startDate), LocalDate.parse(endDate));
        }

//        // 排序条件
//        if (sortField != null && sortOrder != null) {
//            boolean isAsc = "ascending".equalsIgnoreCase(sortOrder);
//            queryWrapper.orderBy(true, isAsc, getSortField(sortField));
//        } else {
//            // 默认按订单日期降序
//            queryWrapper.orderByDesc(Customer::getOrderDate);
//        }

        // 执行查询
        Page<Customer> result = customerService.page(pageInfo, queryWrapper);

        // 构建返回结果
        Map<String, Object> response = new HashMap<>();
        response.put("list", result.getRecords());
        response.put("total", result.getTotal());

        return ResponseEntity.ok(response);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Customer> getCustomerById(@PathVariable Integer id) {
        Customer customer = customerService.getCustomerById(id);
        return customer != null ? ResponseEntity.ok(customer) : ResponseEntity.notFound().build();
    }

    @PostMapping
    public ResponseEntity<?> createCustomer(@RequestBody Customer customer) {
        return customerService.createCustomer(customer) ?
                ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
    }

    @PutMapping("/{id}")
    public ResponseEntity<?> updateCustomer(@PathVariable Integer id, @RequestBody Customer customer) {
        customer.setCustomerId(id);
        return customerService.updateCustomer(customer) ?
                ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteCustomer(@PathVariable Integer id) {
        return customerService.deleteCustomer(id) ?
                ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
    }
}
